import {createRouter, createWebHashHistory} from "vue-router"
import Layout from '@/views/Layout.vue'
import {useDataStore} from "@/stores/dataStore";

const routes = [
    {
        //最常见404匹配规则：没有找到对应路径地址
        path: "/:pathMath(.*)*",
        name: "404",
        component: () => import("../views/home/404.vue"),
        meta: {
            key: "404"
        }
    },
    {
        path: "/home/login",
        name: "login",
        component: () => import("../views/home/Login.vue"),
    },
    {
        //顶级路由
        path: '/',
        name: 'layout',
        redirect: '/home/login',
        component: Layout,
        children: [
            {
                path: "/home/center",
                name: 'home_center',
                component: () => import("../views/home/Center.vue"),
                meta: {
                    requiresAuth: true,
                    key: "个人中心"
                }
            },
            {
                path: "/analysis/data1",
                name: 'analysis_data1',
                component: () => import("../views/analysis/Analysis1.vue"),
                meta: {
                    requiresAuth: true,
                    key: "数据分析"
                }
            },

            {
                path: "/sys/admin",
                name: 'sys_admin',
                component: () => import("../views/sys/Admin.vue"),
                meta: {
                    requiresAuth: true,
                    key: "管理员"
                }
            },
            {
                path: "/sys/role",
                name: 'sys_role',
                component: () => import("../views/sys/Role.vue"),
                meta: {
                    requiresAuth: true,
                    key: "角色"
                }
            },
            {
                path: "/sys/permission",
                name: 'sys_permission',
                component: () => import("../views/sys/Permission.vue"),
                meta: {
                    requiresAuth: true,
                    key: "权限"
                }
            },
            {
                path: "/user/user",
                name: 'user_user',
                component: () => import("../views/user/User.vue"),
                meta: {
                    requiresAuth: true,
                    key: "用户"
                }
            },
            {
                path: "/category/categoryType",
                name: 'category_categoryType',
                component: () => import("../views/category/categoryType.vue"),
                meta: {
                    key: "广告类型"
                }
            },
            {
                path: "/category/category/:name",
                name: 'category_category',
                component: () => import("../views/category/category.vue"),
                meta: {
                    key: "广告"
                }
            },
            {
                path: "/house/house",
                name: 'house_house',
                component: () => import("../views/house/House.vue"),
                meta: {
                    key: "房源"
                }
            },
            {
                path: "/house/houseImg/:name",
                name: 'house_houseImg',
                component: () => import("../views/house/house/HouseImg.vue"),
                meta: {
                    key: "房源图片"
                }
            },
            {
                path: "/house/houseBroker",
                name: 'house_houseBroker',
                component: () => import("../views/house/HouseBroker.vue"),
                meta: {
                    key: "经纪人"
                }
            },
            {
                path: "/house/housePlot",
                name: 'house_housePlot',
                component: () => import("../views/house/HousePlot.vue"),
                meta: {
                    key: "小区"
                }
            },
        ]
    },
]

const router = createRouter({
    routes,
    history: createWebHashHistory(import.meta.env.BASE_URL)
})
//前置导航守卫
// router.beforeEach((to, from, next) => {
//         if (to.meta.requiresAuth) {
//             const dataStore = useDataStore()
//             if (!dataStore.isLogin) {
//                 next({path: "/home/login"})
//             } else {
//                 next()
//             }
//         } else {
//             next()
//         }
//     }
// )


//后置导航
router.afterEach((to, from) => {
    //存储路径(解决页面刷新后 菜单高亮与面包屑不匹配)
    localStorage.setItem("active", to.path)
    //若meta中的key存在，则修改当前面包屑
    if (to.meta.key) {
        const dataStore = useDataStore()
        dataStore.breadcrumb = to.meta.key
    }
})
export default router;
